iT邦幫忙

2021 iThome 鐵人賽

DAY 21
1
Modern Web

Canvas 小錦囊系列 第 21

Day 21 - canvas 玩拼圖 P5.js

  • 分享至 

  • xImage
  •  

今天本來要帶大家玩 用 canvas 完成拼圖

參考的範例在這裡

很可惜又是時間不足 /images/emoticon/emoticon02.gif

先把目前完成的階段貼在這

import "./styles.css";
import Sketch from "react-p5";
import p5 from "p5";
// import Puzzle from "./puzzle";
import { useState } from "react";

let windowWidth = 500;
let windowHeight = 500;

let imgCb = p5.Image;

export default function App() {
  const [puzzle, setPuzzle] = useState(null);
  const preload = (p5) => {
    imgCb = p5.loadImage(
      "https://4.bp.blogspot.com/-2vB_Fmsas04/WTnxS5OAAEI/AAAAAAAAAks/MQg6duIbikUtJmp9curdAi7K5H9ByxQOwCLcB/s640/Screenshot_2017-06-08-22-52-19.png"
    );
  };

  const setup = (p5, canvasParentRef) => {
    p5.createCanvas(imgCb?.width, imgCb?.height);
    p5.image(imgCb, 0, 0);
    // let x0 = windowWidth / 2 - imgCb.width / 2;
    // let y0 = windowHeight / 2 - imgCb.height / 2;
    //  puzzle = new Puzzle(x0, y0, imgCb, 3);
    console.log("imgCb", imgCb);
    // setPuzzle(new Puzzle(x0, y0, imgCb, 3));
  };

  const draw = (p5) => {
    // p5.clear();
    // puzzle.draw();
  };

  const mousePressed = (e) => {
    //   puzzle.mousePressed(e?.mouseX, e?.mouseY);
  };

  const mouseDragged = (event) => {
    // puzzle.mouseDragged(event?.mouseX, event?.mouseY);
  };

  const mouseReleased = () => {
    // puzzle.mouseReleased();
  };

  return (
    <div className="App">
      <Sketch
        preload={preload}
        setup={setup}
        draw={draw}
        mouseReleased={mouseReleased}
        mouseDragged={mouseDragged}
        mousePressed={mousePressed}
      ></Sketch>
    </div>
  );
}

上一篇
Day20 - Fabricjs 與 Image map 仿製 highlight 場地圖 說明
下一篇
Day22 - 用 canvas 做 圈圈叉叉遊戲
系列文
Canvas 小錦囊30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言